#include<iostream>
#include<string.h>
using namespace std;
long long int a[31][31];  // 1e9, 数据比较大，需要更大 的变量，同时为了避免运算中出错，还得时不时取模
long long int n, k, m;
// 定义矩阵的运算：加减，乘法
class Matrix {  
protected:
	int row, //行数
		col; //列数
	long long **arr; //矩阵数组指针
public:
	//Constructor
	Matrix(int row, int col) :row(row), col(col) {
		init(row, col);
	}
 
 

 
	//构造矩阵
void init(int row, int col) {
	if (row && col) {
		arr = new long long int*[row];
		for (int i = 0; i < row; i++) {
			arr[i] = new long long int[col];
			for (int j = 0; j < col; j++)
				arr[i][j] = 0;
		}
	}
}

	void assignment() {
		if (row && col) {
			arr = new long long int*[row];
			for (int i = 0; i < row; i++) {
				arr[i] = new long long int[col];
				for (int j = 0; j < col; j++)
					arr[i][j] = a[i][j];
			}
		}
	}
	void print()
	{
		for(int i = 0; i < row; i++)
		{
			for(int j = 0; j < row; j++)
			{
				cout <<  arr[i][j] % m << " ";
			}
			cout << endl;
		}
	}
	
	void mo()
	{
		for(int i = 0; i < row; i++)
		{
			for(int j = 0; j < row; j++)
			{
				arr[i][j] %= m;
			}
			
		}
	}
	//删除矩阵
	
	//int * Matrix
	friend Matrix operator*(const int n, const Matrix &m) {
		Matrix res(m);
		for (int i = 0; i < res.row; i++)
			for (int j = 0; j < res.col; j++)
				res.arr[i][j] *= n;
		return res;
	}
 
	//Matrix + Matrix
	Matrix operator+(const Matrix &m) {
		Matrix res(m.row, m.col);
		if (row == m.row && col == m.col) {
			for (int i = 0; i < row; i++)
				for (int j = 0; j < col; j++)
					res.arr[i][j] = arr[i][j] + m.arr[i][j];
		}
		return res;
	}
 
	//Matrix * Matrix
	Matrix operator*(const Matrix &m) {
		Matrix res(row, m.col);
		
		for (int i = 0; i < res.row; i++)
			for (int j = 0; j < res.col; j++)
				for (int k = 0; k < res.row; k++)
					res.arr[i][j] += arr[i][k] * m.arr[k][j];
		return res;
	}
};
Matrix quick_power(Matrix m, int k);
Matrix m_sum(Matrix m, int k);


int main()
{
    cin >> n >> k >> m;
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
			cin >> a[i][j];
		}
	}
    Matrix m1(n, n);
	m1.assignment();  // 对m1赋值
	m1 = m_sum(m1, k);
	m1.print();
    return 0;
}

// 计算A^n
Matrix quick_power(Matrix m, int k)
{
	if (k == 1) return m;
	bool tmp = (k % 2 == 0);
	Matrix m1 = quick_power(m, k/2);
	m1.mo();
	m1 = m1 * m1;
	m1.mo();
	if (tmp) return m1;
	else 
	{
		Matrix m2 = m1*m;
		m2.mo();
		return m2;
	}
}


// 计算A^1 + A^2 + A^3 +...+A^k
Matrix m_sum(Matrix m, int k)
{
	if(k == 1) return m;
	bool tmp = (k % 2 == 0);  // 分奇偶
	Matrix m1 = m_sum(m, k/2);
	m1.mo();
	Matrix m2 = m1 + quick_power(m, k/2+1);
	m2.mo();
	if(tmp)
	{
		Matrix m3 = m1 + quick_power(m, k/2) * m1;
		m3.mo();
		return m3;
	}
	else
	{
		Matrix m4 = m1 + quick_power(m, k/2) * m2;
		m4.mo();
		return m4;
	}
}